package com.example;

import com.example.realm.CustomRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;

/**
 * @ClassName QuickStartTest5_4
 * @Description TODO
 * @Author zhaoluowei
 * @Date 2019/7/1  22:43
 * @Version 1.0
 */

public class QuickStartTest5_4 {

    private CustomRealm customRealm = new CustomRealm();

    private DefaultSecurityManager securityManager = new DefaultSecurityManager();

    @Before
    public void init(){

        //构建环境
        securityManager.setRealm(customRealm);
        SecurityUtils.setSecurityManager(securityManager);
    }

    @Test
    public void test(){
        //获取当前 操作的主体
        Subject subject = SecurityUtils.getSubject();
        //构造需要登录用户信息 用户用的
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("jack", "123");


        //断点开始的地方
        subject.login(usernamePasswordToken);
        System.out.println(" 认证结果 -->  " + subject.isAuthenticated());
        System.out.println(" Principal  -->  " + subject.getPrincipal());

        System.out.println("是否有 role1 角色 --> "+subject.hasRole("role1"));
        System.out.println("是否有 role2 角色 --> "+subject.hasRole("role2"));

        System.out.println("是否有 buy 权限 --> "+subject.isPermitted("buy"));
        System.out.println("是否有 query 权限 --> "+subject.isPermitted("query"));
        System.out.println("是否有 add 权限 --> "+subject.isPermitted("add"));
    }

}
